
using System.Collections.Generic;
using System.IO;

public  partial class Localization
{
    private static Localization _instance;
    public static Localization instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new Localization();

                _instance.LoadData();
            }
            
            return _instance;
        }
        
    }

    private const string localizationDataDir = "Data/Shared/Localization";
    
    Dictionary<string,string> m_Dict= new Dictionary<string, string>();

    void LoadData()
    {
/*        #if !NETCOREAPP
        if(ConstData.Instance == null)
            ConstData.LocalInitConstData();
        #endif
        foreach (var lang in ConstData.Instance.langs)
        { 
            string  key = lang.key;
            string value = "";
            switch(language)
           {
               case LanguageType.CN: value = lang.CN; break;
               case LanguageType.EN: value = lang.EN; break;
               case LanguageType.TC: value = lang.TC; break;
           }

            m_Dict[key] = value;
        }*/
        

        string resPath = Path.Combine(localizationDataDir, language.ToString());
        var file = UnityEngine.Resources.Load<UnityEngine.TextAsset>(resPath);
        if (file != null)
        {
            string jsonStr = file.text;
            m_Dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
        }
        else
        {
            GameDebug.LogError($"cannot find localization data file：{language}");
        }
        //string localizationDataFilePath = "../Data/Shared/Localization/CN.json";

        //if (!System.IO.File.Exists(localizationDataFilePath))
        //{
        //    GameDebug.LogError("语言文件不存在");
        //}
        //try
        //{
        //    var text = System.IO.File.ReadAllText(localizationDataFilePath);
        //    m_Dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(text);
        //}
        //catch (System.Exception e)
        //{
        //    GameDebug.LogError(e);
        //}

    }
    
    public string GetValue(string key)
    {
        if (m_Dict.ContainsKey(key))
        {
            string value = m_Dict[key];
            value = value.Replace("\\n", "\n");
            return value;
        }
        else
        {
            GameDebug.LogError($"找不到语言值：{key}");
            return $"Invalid key:{key}";
        }
    }
    public string GetValue(string key,out bool get)
    {
        if (m_Dict.ContainsKey(key))
        {
            get = true;
            string value = m_Dict[key];
            value = value.Replace("\\n", "\n");
            return value;
        }
        else
        {
            get = false;
            GameDebug.LogError($"找不到语言值：{key}");
            return $"Invalid key:{key}";
        }
    }
}